package com.china08.yxyapi.api.manage.sch.platform.resource.yxylectureprepack;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLectureFeedBackRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.service.db.YxyLecturePrePackService;
import com.china08.yxyapi.util.DateToolUtils;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLectureSchPrePackStatusController extends YxyApiBaseController {

    Logger log = Logger.getLogger(YxyLectureSchPrePackStatusController.class);

    @Autowired
    YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @Autowired
    YxyLectureFeedBackRepository yxyLectureFeedBackRepository;

    @Autowired
    YxyLecturePrePackService yxyLecturePrePackService;

    @SuppressWarnings("unchecked")
    @ApiOperation(value = "")
    @RequestMapping(value = "/api_yw/manage/sch/platform/resource/lectureprepack/status/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String phaseId = reqModel.phaseId;
        String subjectId = reqModel.subjectId;
        String keyword = reqModel.keyword;
        Integer status = reqModel.status;
        Integer order = reqModel.order;

        YxyProfile yxyProfile = getProfile(request, response);

        // 当前学校
        String schoolId = yxyProfile.getSchoolId();
        // String areaIdProvince = yxyProfile.getSchoolAreaIdProvince();
        // String areaIdCity = yxyProfile.getSchoolAreaIdCity();
        // String areaIdDistrict = yxyProfile.getSchoolAreaIdDistrict();
        // Integer bureauLevel = yxyProfile.getSchoolBureauLevel();

        // 云校schoolId
        // String yunSchoolId = "ff80808142a16af00142a1a60f010006";

        Map<String, Object> map = new HashMap<String, Object>();
        List<YxyLecturePrePack> list = new ArrayList<>();
        long total = 0L;

        // if (schoolId.equals(yunSchoolId)) {
        // log.info("云校用户");
        // map = yxyLecturePrePackService.findForYunXiao(phaseId, subjectId,
        // keyword, status, order, reqModel.page, reqModel.size);
        // list = (List<YxyLecturePrePack>) map.get("list");
        // total = (long) map.get("count");
        // }
        //
        // else {
        // log.info("非云校用户");
        map = yxyLecturePrePackService.findForSCH(schoolId, phaseId, subjectId, status, order, keyword, reqModel.page, reqModel.size);
        list = (List<YxyLecturePrePack>) map.get("list");
        total = (long) map.get("count");
        // }

        List<Pojo> returnList = new ArrayList<>();

        for (YxyLecturePrePack yxyLecturePrePack : list)

        {
            String id = yxyLecturePrePack.getId();
            String packName = yxyLecturePrePack.getPackName();
            String subjectName = yxyLecturePrePack.getSubjectName();
            String phaseName = yxyLecturePrePack.getPhaseName();
            String versionName = yxyLecturePrePack.getVersionName();
            String semesterName = yxyLecturePrePack.getSemesterName();
            String bookcontentsName = yxyLecturePrePack.getBookcontentsName();
            String userNick = yxyLecturePrePack.getUserNick();
            String schoolNick = yxyLecturePrePack.getSchoolNick();
            String downReason = yxyLecturePrePack.getDownReason();
            String auditUserName = yxyLecturePrePack.getAuditUserName();
            String auditSchoolName = yxyLecturePrePack.getAuditSchoolName();
            Date date = yxyLecturePrePack.getCreatedDate();
            String createdDate = "";
            if (null != date) {
                createdDate = DateToolUtils.format(date, "yyyy-MM-dd");
            }
            List<Res> resources = yxyLecturePrePack.getResources();
            long feedCount = 0;
            // 下架备课包才有举报数
            if (status == 0) {
                feedCount = yxyLectureFeedBackRepository.countByPrepackId(id);
            }
            Pojo pojo = new Pojo();
            pojo.id = id;
            pojo.packName = packName;
            pojo.subjectName = subjectName;
            pojo.phaseName = phaseName;
            pojo.versionName = versionName;
            pojo.semesterName = semesterName;
            pojo.bookcontentsName = bookcontentsName;
            pojo.userNick = userNick;
            pojo.schoolNick = schoolNick;
            pojo.createdDate = createdDate;
            pojo.resources = resources;
            pojo.feedCount = feedCount;
            pojo.downReason = downReason;
            pojo.auditUserName = auditUserName;
            pojo.auditSchoolName = auditSchoolName;

            returnList.add(pojo);
        }

        RespModel respModel = new RespModel();
        respModel.list = returnList;
        respModel.total = total;
        return respModel;
    }

    public static class Pojo {
        public String id;
        public String packName;
        public String phaseName;
        public String subjectName;
        public String versionName;
        public String semesterName;
        public String bookcontentsName;
        public String userNick;
        public String schoolNick;
        public String createdDate;
        // 下架原因
        public String downReason;
        public List<Res> resources;
        public long feedCount;
        // 审核人
        public String auditUserName;
        // 审核人学校
        public String auditSchoolName;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String phaseId;
        public String subjectId;
        public String keyword;
        public Integer status;
        // 排序 正序：1 倒序：0 默认：0
        public Integer order;
    }

    protected static class RespModel extends YxyApiRespModel {
        public List<Pojo> list;
        public long total;
    }
}
